/**
 * 
 * @Author: biubiu0002
 * @Date: 2021-04-26 11:41
 **/



#include "StudentScore.h"
#include "StudentScoreMapper.h"
#include "list"
#include "iostream"
#include "iomanip"
#include "fstream"
#include "sstream"
#include "string"
#include "vector"
using namespace std;


vector<StudentScore> StudentScoreMapper::selectByStudentId(string studentId){
    list<StudentScore> studentScoreList =  getStudentScoreList();
    vector<StudentScore> resultList;
    for(StudentScore studentScore:studentScoreList){
        if(studentScore.getStudentId()==studentId){
            resultList.push_back(studentScore);
        }
    }
    return resultList;

}

vector<StudentScore> StudentScoreMapper::selectByCourseId(string courseId){
    list<StudentScore> studentScoreList = getStudentScoreList();
    vector<StudentScore> resultList;
    for(StudentScore studentScore:studentScoreList){
        if(studentScore.getCourseId() ==courseId){
            resultList.push_back(studentScore);
        }
    }
    return resultList;

}

list<StudentScore> StudentScoreMapper::loadStudentScoreMapper() {
    list<StudentScore> studentScoreList;
    string line,field;
    ifstream ifs("../../resource/StudentScore.txt",ios::in);
    if(!ifs){
        cout<<"StudentScore.txt文件打开失败！"<<endl;

    }
    while(getline(ifs,line)){
        StudentScore studentScore;
        vector<string>infoList;
        istringstream iss(line);
        while(iss>>field){
            infoList.push_back(field);
        }
        studentScore.setStudentId(infoList[0]);
        studentScore.setCourseId(infoList[1]);
        studentScore.setSchoolYear(stoi(infoList[2]));
        studentScore.setSemester(stoi(infoList[3]));
        studentScore.setRegularScore(stof(infoList[4]));
        studentScore.setExamScore(stof(infoList[5]));
        studentScore.setFinalScore(stof(infoList[6]));
        studentScoreList.push_back(studentScore);
    }
    ifs.close();
    return studentScoreList;
}

int StudentScoreMapper::batchInsertStudentScore(list<StudentScore> &studentScoreList) {
    ofstream ofs("../../resource/StudentScore.txt",ios::out);
    for(StudentScore studentScore:studentScoreList){
        ofs<<studentScore.getStudentId()<<" ";
        ofs<<studentScore.getCourseId()<<" ";
        ofs<<studentScore.getSchoolYear()<<" ";
        ofs<<studentScore.getSemester()<<" ";
        ofs<<fixed<<setprecision(2)<<studentScore.getRegularScore()<<" ";
        ofs<<fixed<<setprecision(2)<<studentScore.getExamScore()<<" ";
        ofs<<fixed<<setprecision(2)<<studentScore.getFinalScore()<<endl;

    }
    return 0;
}

StudentScoreMapper::StudentScoreMapper() {
    setStudentScoreList(loadStudentScoreMapper());
}

const list<StudentScore> &StudentScoreMapper::getStudentScoreList() const {
    return studentScoreList;
}

void StudentScoreMapper::setStudentScoreList(const list<StudentScore> &studentScoreList) {
    StudentScoreMapper::studentScoreList = studentScoreList;
}

StudentScoreMapper::~StudentScoreMapper() {

}
